package com.fzrskj.cloud.free.core.utils;

import com.gargoylesoftware.htmlunit.BrowserVersion;
import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.html.HtmlPage;
import org.apache.commons.collections.map.HashedMap;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * HtmlUtils.
 *
 * @author zj.
 *         Created on 2019/5/10 0010.
 */
public class HtmlUtils {
    private static Map<String, String> contentCache = new HashedMap();

    public static String agentHtml (String webUrl) {
        String content = "";
        if (contentCache.containsKey(webUrl)) {
            content = contentCache.get(webUrl);
            new Thread(new Runnable() {
                @Override
                public void run() {
                    String html = getHtmlFromUrl(webUrl);
                    contentCache.put(webUrl, html);
                }
            }).start();
        } else {
            content = getHtmlFromUrl(webUrl);
            contentCache.put(webUrl, content);
        }
        return content;
    }

    public static String getHtmlFromUrl(String url) {
        return getHtmlFromUrl(url, 10);
    }

    public static String getHtmlFromUrl(String url, Integer timeout) {
        HtmlPage page = getHtmlPage(url, timeout);

        Document doc = Jsoup.parse(page.asXml());
        String result = doc.html().replace("&nbsp;", "");

        return result;
    }

    private static HtmlPage getHtmlPage(String url) {
        return getHtmlPage(url, 10);
    }

    private static HtmlPage getHtmlPage(String url, Integer timeout) {
        try {
            final WebClient webClient = new WebClient(BrowserVersion.CHROME);
            webClient.getOptions().setCssEnabled(false);
            webClient.getOptions().setJavaScriptEnabled(false);
            webClient.getOptions().setThrowExceptionOnScriptError(false);
            webClient.getOptions().setMaxInMemory(500);
            final HtmlPage page = webClient.getPage(url);
            //web请求数据需要时间，必须让主线程休眠片刻
            TimeUnit.SECONDS.sleep(timeout);
            webClient.close();

            return page;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}
